<script>
  class EventBus {
    constructor() {
      // subs = {eat:[fn1,fn2],失恋：[fn1,fn2]}
      this.subs = {};
    }
    $on(eventType, fn) {
      if (!this.subs[eventType]) {
        this.subs[eventType] = [];
      }
      this.subs[eventType].push(fn)
    }
    $emit(eventType) {
      if (this.subs[eventType]) {
        this.subs[eventType].forEach(fn => {
          fn();
        })
      }
    }
  }

  // 事件总线（发布订阅）
  // {eat:[fn1,fn2]}  {失恋：[fn1,fn2]}
  let eb = new EventBus();
  // 订阅是可以订阅多次的
  eb.$on("eat", () => {
    console.log("eat事件发生了1~");
  })
  eb.$on("eat", () => {
    console.log("eat事件发生了2~");
  })
  // 发布
  eb.$emit("eat");

  // ----------------------------- 
  // 订阅
  eb.$on("失恋", () => {
    console.log("喝酒~");
  })
  eb.$on("失恋", () => {
    console.log("睡觉~");
  })
  eb.$emit("失恋")

</script>